package com.yami.shop.admin.controller;


import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.VipPackage;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.UserMapper;
import com.yami.shop.service.VipPackageService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 * vip套餐 前端控制器
 * </p>
 *
 * @author chajiu
 * @since 2021-06-29
 */
@RestController
@RequestMapping("/vip-package")
public class VipPackageController {


    @Autowired
    private com.yami.shop.service.VipPackageService VipPackageService;
    @Autowired
    private UserMapper userMapper;

    /**
     * 分页获取
    */
     @GetMapping("/page")
     @PreAuthorize("@pms.hasPermission('admin:VipPackage:page')")
     public ResponseEntity<IPage<VipPackage>> page(VipPackage entity, PageParam<VipPackage> page) {
         IPage<VipPackage> iPage = VipPackageService.page(page, new LambdaQueryWrapper<VipPackage>()
         .orderByAsc(VipPackage::getPrice));//价格升序
         //.eq(VipPackage::getMark,1));//按标识获取 1为普通套餐
         return ResponseEntity.ok(iPage);
     }

    /**
     * 分页获取营销管理，新人优惠
     */
    @GetMapping("/pageNew")
    //@PreAuthorize("@pms.hasPermission('admin:VipPackage:pageNew')")
    @ApiOperation(value = "获取新人优惠",notes = "获取新人优惠")
    public ResponseEntity<IPage<VipPackage>> pageNew(VipPackage entity, PageParam<VipPackage> page) {
        IPage<VipPackage> iPage = VipPackageService.page(page, new LambdaQueryWrapper<VipPackage>()
                .orderByAsc(VipPackage::getPrice)//价格升序
                .eq(VipPackage::getMark,0));//按标识获取 0为新人优惠
        return ResponseEntity.ok(iPage);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('admin:VipPackage:save')")//保存时mark传入0标识营销管理的新人优惠，1表示普通VIP套餐
    public ResponseEntity<Void> save(@RequestBody VipPackage entity) {
        VipPackageService.save(entity);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@pms.hasPermission('admin:VipPackage:info')")
    public ResponseEntity<VipPackage> info(@PathVariable("id") String id){
        VipPackage entity = VipPackageService.getById(id);
            return ResponseEntity.ok(entity);
    }

    /**
     * 修改
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin:VipPackage:update')")
    public ResponseEntity<Void> update(@RequestBody VipPackage entity){
        VipPackageService.updateById(entity);
            return ResponseEntity.ok().build();
    }

    /**
     * 赠送用户优惠
     */
    @PutMapping("/editUser")
    //@PreAuthorize("@pms.hasPermission('admin:VipPackage:update')")
    @ApiOperation(value = "赠送指定用户优惠",notes = "赠送指定用户优惠,需要手机号和套餐id")
    public ResponseEntity<Void> editUser(String Mobile,Integer vipId){
        User user = new User();
        user.setDiscount(vipId);
        userMapper.update(user,new LambdaQueryWrapper<User>()
        .eq(User::getUserMobile,Mobile));//修改user
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('admin:VipPackage:delete')")
    public ResponseEntity<Void> delete(@RequestBody String[]ids){
        VipPackageService.removeByIds(Arrays.asList(ids));
            return ResponseEntity.ok().build();
    }


}

